package com.cb.kfc.dao;

import com.cb.kfc.domain.Member;
import com.cb.kfc.domain.Orders;
import com.cb.kfc.domain.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface IOrdersDao {

    @Select("select * from orders")
    @Results({
            @Result(id=true,column = "id",property = "id"),
            @Result(column = "orderNum",property = "orderNum"),
            @Result(column = "orderTime",property = "orderTime"),
            @Result(column = "orderDesc",property = "orderDesc"),
            @Result(column = "productId",property = "product",one = @One(select = "com.cb.kfc.dao.IProductDao.findOne"))
    })
    public List<Orders> findAll() throws Exception;

//    id, ordernum, ordertime,  orderdesc,  productid, memberid
    @Insert("insert into orders(ordernum,ordertime,orderdesc,productid,memberid) values(#{orderNum},#{orderTime},#{orderDesc},#{productId},#{memberId})")
    void save(Orders orders);



    //删除单个，或者批量删除
    @Delete("<script>delete from orders where id in <foreach collection='array' item='id' open='(' separator=',' close=')'>#{id}</foreach> </script>")
    Integer delete(String[] ids);

    @Select("select * from orders where id =#{id}")
    @Results({
            @Result(id=true,column = "id",property = "id"),
            @Result(column = "orderNum",property = "orderNum"),
            @Result(column = "orderTime",property = "orderTime"),
            @Result(column = "orderDesc",property = "orderDesc"),
            @Result(column = "productId",property = "product",javaType = Product.class,one = @One(select = "com.cb.kfc.dao.IProductDao.findOne")),
            @Result(column = "memberId",property = "member",javaType = Member.class,one = @One(select = "com.cb.kfc.dao.IMemberDao.findOne")),
            @Result(column = "id",property = "friend",javaType = java.util.List.class,many = @Many(select = "com.cb.kfc.dao.IFriendDao.findByOrdersId")),
//最后一行，column = "id"就是使用本表orders的id，通过中间表，查询friend，是一个多对多的关系，返回多个，返回集合List

    })
    Orders findById(String id);
}
